package com.basswold.nasaesa.client;
import com.basswold.nasaesa.client.BuildingWingFactory.FactoryType;
import com.basswold.nasaesa.client.ConstructionModule.CardinalDirection;
import com.basswold.nasaesa.client.ConstructionModule.ModuleType;

/**
 * Used to build a building wing of a mars base.
 * A food wing contains canteen and food/water storage modules.
 * @author Keith
 *
 */	
public class FoodWingFactory extends BuildingWingFactory {

	public FoodWingFactory(ContructionList moduleList) {
		super(moduleList);
	}

	@Override
	protected ModuleType[] getAcceptableModules() {
		return new ConstructionModule.ModuleType[]
	         {ConstructionModule.ModuleType.CANTEEN,
			 ConstructionModule.ModuleType.FOOD_AND_WATER};
	}

	@Override
	public ConstructionConnectorModule startConstruction(CardinalDirection direction,
			boolean airlockIncluded) {
		ConstructionConnectorModule connector = null;
		
		try {
			if (airlockIncluded 
				&& modules.hasModuleOfType(ModuleType.AIRLOCK)
				&& modules.hasModuleOfType(ModuleType.PLAIN)) {
				connector = (ConstructionConnectorModule) modules.getModuleOfType(
						ConstructionModule.ModuleType.PLAIN);
				this.setEndConnection(connector, 
						modules.getModuleOfType(ModuleType.AIRLOCK), direction);
				// Add a medical module if possible
				connector = this.addModule(connector, 
						modules.getModuleOfType(ModuleType.MEDICAL), direction);
			} else if (modules.hasModuleOfType(ModuleType.PLAIN)) {
				connector = (ConstructionConnectorModule) modules.getModuleOfType(
						ConstructionModule.ModuleType.PLAIN);
				ConstructionModule[] array = this.getModule(1);
				if (array[0] != null) {
					this.setEndConnection(connector, array[0], direction);
				} else {
					//array way null, nothing can be added
					modules.addModule(connector);
					connector = null;
				}
			} else {
				// cannot create a food wing
			}
		} catch (Exception ex) {
			// This should never happen, since we are making sure that all
			// modules needed for construction are present before beginning
			// construction.
			return null;
		}
		
		return connector;
	}

	@Override
	public FactoryType getType() {
		return FactoryType.FoodWingFactory;
	}

}
